package com.tio.demo;

import org.tio.client.AioClient;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Aio;
import org.tio.core.Node;

/**
 * Author      : liyuemin
 * Time        : 2017/12/14 14:50
 * Description :
 */
public class HelloClientStarter {
    //服务器节点
    public static Node serverNode = new Node(Const.SERVER,Const.PORT);
    //handler
    public static ClientAioHandler aioClientHandler = new HelloClientHandler();
    //事件监听
    public static ClientAioListener aioListener = null;
    //断开连接后自动连接
    private static ReconnConf reconnConf = new ReconnConf(5000L);
    //一组连接公用上下文的对象
    private static ClientGroupContext clientGroupContext = new ClientGroupContext(aioClientHandler,aioListener,reconnConf);

    public static AioClient aioClient = null;
    public static ClientChannelContext clientChannelContext = null;

    //启动程序入口
    public static void main(String [] args ) throws Exception{
        clientGroupContext.setHeartbeatTimeout(Const.TIMEOUT);
        aioClient = new AioClient(clientGroupContext);
        clientChannelContext = aioClient.connect(serverNode);
        send();
    }

    private static void send () throws  Exception{
        HelloPacket packet = new HelloPacket();
        packet.setBody("hello world ".getBytes(HelloPacket.CHARSTER));
        Aio.send(clientChannelContext,packet);
    }

}
